import { getStorage } from '../../../components/utils'
import MyUtils from '../../components/personal/utils'
import Taro, { getFileInfo } from '@tarojs/taro'

if(process.env.TARO_ENV === 'weapp'){
    var app = Taro.getApp().$app
}

const storage = getStorage()

function getToken(){//token获取
    if(process.env.TARO_ENV === 'weapp'){
        return app.globalData.Token
    }else if(process.env.TARO_ENV === 'h5'){
        if(getEnv() === 'weapp'){//嵌入小程序的H5
            return MyUtils.getStorage("lg_tk") || MyUtils.getQueryVariable("lg_tk")
        }
        let token = storage.get("token")
        if(!token){
            let urlToken = MyUtils.getQueryVariable("lg_tk")
            return urlToken
        }else{
            return storage.get("token")
        }
    }else{
        console.log("未配置端,Token获取失败")
        return ""
    }
}

function getUserType(){//获取用户类型，0-学校管理员，1-老师，2-学生，3-家长，6-超级管理员，7-学校领导，8-教育局领导，11-其他教职工
    if(process.env.TARO_ENV === 'weapp'){
        return app.globalData.userType
    }else if(process.env.TARO_ENV === 'h5'){
        if(getEnv() === 'weapp'){//嵌入小程序的H5
            return MyUtils.getQueryVariable("userType") || MyUtils.getStorage("userInfo")?.userType 
        }
        let userInfo = storage.get("userInfo")?JSON.parse(storage.get("userInfo")):undefined
        return userInfo?.UserType || MyUtils.getQueryVariable("userType") || MyUtils.getStorage("userInfo")?.userType 
    }else{
        console.log("未配置端,userType获取失败")
        return ""
    }
}

function getUserId(){//获取用户id
    if(process.env.TARO_ENV === 'h5'){
        if(getEnv() === 'weapp'){//嵌入小程序的H5
            return MyUtils.getStorage("userInfo")?.userId 
        }else{
            if(storage.get("userInfo")){
                return JSON.parse(storage.get("userInfo"))?.UserID
            }
        }
        let userInfo = storage.get("userInfo")?JSON.parse(storage.get("userInfo")):undefined
        return userInfo?.UserID || MyUtils.getStorage("userInfo")?.userId
    }else if(process.env.TARO_ENV === 'weapp'){
        return app.globalData.userID
    }
    return undefined
}

function getUserName(){//获取用户姓名
    if(process.env.TARO_ENV === 'weapp'){
        let userInfo = MyUtils.getStorage("userInfo")
        return userInfo.userName || app.globalData.userName
    }else if(process.env.TARO_ENV === 'h5'){
        if(getEnv() === 'weapp'){//嵌入小程序的H5
            return MyUtils.getStorage("userInfo")?.userName 
        }
        let userInfo = MyUtils.getStorage("userInfo")
        return userInfo.userName  
    }
}

function getBaseUrl(){//获取基础平台地址
    if(process.env.TARO_ENV === "h5"){
        if(getEnv() === "weapp"){//小程序嵌入H5
            return MyUtils.getStorage("baseUrl")
        }else{//app嵌入H5
            return MyUtils.getStorage("schoolSystemInfo")?.baseUrl
        }
    }else if(process.env.TARO_ENV === "weapp"){
        return app.globalData.schoolUrl
    }
}

function getIdentityCode(){//获取当前身份
    if(process.env.TARO_ENV === 'weapp'){
        let identityInfos = MyUtils.getStorage("userInfo").identityList
        let identityCode = ""
        if(MyUtils.getStorage("lg_ic")){
            identityCode = MyUtils.getStorage("lg_ic")
        }else if(storage.get("lg_ic")){
            identityCode = storage.get("lg_ic")
        }else if(identityInfos){
            identityCode = identityInfos[0]?.identityCode
        }
        identityCode = haveIdentityCode(identityCode,identityInfos)
        return identityCode
    }else if(process.env.TARO_ENV === "h5"){
        let identityInfos = MyUtils.getStorage("userInfo")?.identityList
        let identityCode:any = ""
        if(getEnv() === 'weapp'){//嵌入小程序的H5
            if(MyUtils.getQueryVariable("lg_ic")){
                identityCode = MyUtils.getQueryVariable("lg_ic")
            }else if(identityInfos){
                identityCode = identityInfos[0]?.identityCode
            } 
        }else{
            if(storage.get("lg_ic")){
                identityCode = storage.get("lg_ic")
            }else if(MyUtils.getQueryVariable("lg_ic")){
                identityCode = MyUtils.getQueryVariable("lg_ic")
            }else if(identityInfos){
                identityCode = identityInfos[0]?.identityCode
            }
        }
        identityCode = haveIdentityCode(identityCode,identityInfos)
        return identityCode   
    }
}

function getIdentity(){
    if(process.env.TARO_ENV === 'weapp'){
        let identityInfos = MyUtils.getStorage("userInfo").identityList
        let identityCode = ""
        if(MyUtils.getStorage("lg_ic")){
            identityCode = MyUtils.getStorage("lg_ic")
        }else if(storage.get("lg_ic")){
            identityCode = storage.get("lg_ic")
        }else if(identityInfos){
            identityCode = identityInfos[0]?.identityCode
        }
        let identity = getIdentityObj(identityCode,identityInfos)
        return identity
    }else if(process.env.TARO_ENV === "h5"){
        let identityInfos = MyUtils.getStorage("userInfo")?.identityList
        let identityCode:any = ""
        if(getEnv() === 'weapp'){//嵌入小程序的H5
            if(MyUtils.getQueryVariable("lg_ic")){
                identityCode = MyUtils.getQueryVariable("lg_ic")
            }else if(identityInfos){
                identityCode = identityInfos[0]?.identityCode
            } 
        }else{
            if(storage.get("lg_ic")){
                identityCode = storage.get("lg_ic")
            }else if(MyUtils.getQueryVariable("lg_ic")){
                identityCode = MyUtils.getQueryVariable("lg_ic")
            }else if(identityInfos){
                identityCode = identityInfos[0]?.identityCode
            }
        }
        let identity = getIdentityObj(identityCode,identityInfos)
        return identity   
    }
}

//判断是否有该身份
function haveIdentityCode(identityCode,identityInfos){
    if(!identityInfos){//身份数组没有判断不了，直接返回
        return identityCode
    }else{
        if(identityInfos.findIndex(item=>item.identityCode == identityCode) == -1){
            console.log("个人身份列表中不存在该身份",identityCode)
            return identityInfos[0].identityCode
        }else{
            return identityCode
        }
    }
}

//
function getIdentityObj(identityCode,identityInfos){
    let identity = identityInfos.find(item=>item.identityCode == identityCode)
    if(!identity){
        console.log("个人身份列表中不存在该身份",identityCode)
        return identityInfos[0]
    }else{
        return identity
    }
}

function getSchoolType(){//获取学校类型，1/大学，2/中小学
    return MyUtils.getStorage("schoolSystemInfo").schoolType
}

function getSchoolId(){//获取学校ID
    return MyUtils.getQueryVariable("schoolId") || MyUtils.getStorage("userInfo")?.eduId
}

function getSchoolName(){//获取学校名字
    return MyUtils.getStorage("schoolSystemInfo").schoolName
}

function getSysId(){//获取系统ID
    return MyUtils.getStorage("schoolSystemInfo").noticeSysId
}

function getOpenId(){//获取小程序的openId
    if(process.env.TARO_ENV === 'weapp'){
        return app.globalData.openID
    }
    return ""
}
function whichWay(){//哪一个来源
    if(process.env.TARO_ENV === 'weapp'){
      return 3
    }else if(process.env.TARO_ENV === 'h5'){
      return 2
    }else{
      return 3
    }
}
function getEnv(){
    return MyUtils.getQueryVariable("env")
}

function getCollegeId(){
    if(process.env.TARO_ENV === 'h5'){
        if(getEnv() === 'weapp'){//嵌入小程序的H5
            return MyUtils.getStorage("userInfo")?.collegeId 
        }else{
            if(storage.get("userInfo")){
                return JSON.parse(storage.get("userInfo"))?.CollegeID
            }
        }
        let userInfo = storage.get("userInfo")?JSON.parse(storage.get("userInfo")):undefined
        return userInfo?.CollegeID || MyUtils.getStorage("userInfo")?.collegeId
    }else if(process.env.TARO_ENV === 'weapp'){
        return app.globalData.CollegeID
    }
}

function getCollegeName(){
    if(process.env.TARO_ENV === 'h5'){
        if(getEnv() === 'weapp'){//嵌入小程序的H5
            return MyUtils.getStorage("userInfo")?.collegeName
        }else{
            if(storage.get("userInfo")){
                return JSON.parse(storage.get("userInfo"))?.CollegeName
            }
        }
        let userInfo = storage.get("userInfo")?JSON.parse(storage.get("userInfo")):undefined
        return userInfo?.CollegeName || MyUtils.getStorage("userInfo")?.collegeName
    }else if(process.env.TARO_ENV === 'weapp'){
        return app.globalData.CollegeName
    }
}

export default {
    /**获取token值 */
    getToken,
    /**获取用户类型 */
    getUserType,
    /**获取用户id */
    getUserId,
    /**获取当前身份 */
    getIdentityCode,
    /**获取当前身份的信息对象 */
    getIdentity,
    /**获取用户姓名 */
    getUserName,

    /**获取基础平台地址 */
    getBaseUrl,
    /**获取学校类型 */
    getSchoolType,
    /**获取学校ID */
    getSchoolId,
    //获取学校名字
    getSchoolName,
    /**获取系统ID */
    getSysId,
    /** 获取院系ID */
    getCollegeId,
    /**  获取当前学院名 */
    getCollegeName,

    /**获取小程序的openId(非小程序返回空字符串) */
    getOpenId,
    /**判断运行环境 3-weapp；2-h5*/
    whichWay,
    /**判断是否是微信小程序嵌入的H5 */
    getEnv
}